package com.puman.beautyfarmcounselor.ui.setting;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.puman.beautyfarmcounselor.IndexActivity;
import com.puman.beautyfarmcounselor.MainActivity;
import com.puman.beautyfarmcounselor.R;
import com.puman.beautyfarmcounselor.http.AjaxCallBack;
import com.puman.beautyfarmcounselor.ui.core.BaseFragment;
import com.puman.beautyfarmcounselor.ui.setting.adpter.MsgListAdapter;
import com.puman.beautyfarmcounselor.ui.setting.model.Msg;
import com.puman.beautyfarmcounselor.util.XTHttp;
import com.puman.beautyfarmcounselor.util.config.GlobalData;
import com.puman.beautyfarmcounselor.util.config.RequestAddr;
import com.puman.beautyfarmcounselor.util.view.MultiSwipeRefreshLayout;
import com.puman.beautyfarmcounselor.util.view.SwipeRefreshLayout;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;

/**
 * 作者：sharpX on 15/7/2 10:39
 * 邮箱：634073349@qq.com.com
 * 消息列表
 */
@EFragment(R.layout.msg_linearlayout)
public class MegListFragment extends BaseFragment implements android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener, com.puman.beautyfarmcounselor.util.view.SwipeRefreshLayout.OnLoadListener{
    @ViewById(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @ViewById(R.id.base_title)
    TextView title;

    @ViewById(R.id.multswiperefresh)
    MultiSwipeRefreshLayout mSwipeRefreshLayout;
    @ViewById(R.id.swiperefresh)
    SwipeRefreshLayout swipeRefreshLayout;

    @ViewById(R.id.back_btn)
    ImageView backBtn;

     MsgListAdapter msgListAdapter;


    @AfterViews
    void initView() {
        /**
         * 设置layoutmanager
         */
        //设置子布局
        mSwipeRefreshLayout.setSwipeableChildren(R.id.recyclerView);
        //设置监听
        mSwipeRefreshLayout.setOnRefreshListener(this);

        swipeRefreshLayout.setOnLoadListener(this);
        swipeRefreshLayout.setColor(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
//        swipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        swipeRefreshLayout.setLoadNoFull(false);
        backBtn.setVisibility(View.VISIBLE);
        title.setText("消息");
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(layoutManager);
//        list.addAll(getData((int)Math.random()*100));
//        temps.addAll( getData(new Random(10).nextInt()));

        getData();


    }
    XTHttp xtHttp = new XTHttp();
    String url = "";
    String param = "";
    int pageNum = 1;
    int currentPage = 1;

    private void getData() {
        param = "emp_id=" + GlobalData.counselor.getData().getEmployee_id()+
        "&msg_type="+1+
        "&pageNumber="+pageNum;
        url = RequestAddr.message + param;
        xtHttp.get(url, new AjaxCallBack<Object>() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                currentPage = pageNum;
                dealData(o);
                mSwipeRefreshLayout.setRefreshing(false);
                swipeRefreshLayout.setLoading(false);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                Toast.makeText(IndexActivity.getInstace(), "网络不稳定，请稍后再试", Toast.LENGTH_SHORT).show();
                super.onFailure(t, errorNo, strMsg);
//                mSwipeRefreshLayout.setRefreshing(false);
                mSwipeRefreshLayout.setRefreshing(false);
                swipeRefreshLayout.setLoading(false);
            }

            @Override
            public void onLoading(long count, long current) {
                super.onLoading(count, current);
                if (count == current){
                    mSwipeRefreshLayout.setRefreshing(false);
                    swipeRefreshLayout.setLoading(false);
                }
            }
        });
    }

    Msg msg;
    Msg msgTemp = new Msg();
    private void dealData(Object o) {
        ObjectMapper mapper = new ObjectMapper();
        if (currentPage == 1 && msg != null){
            msg.getData().getMessageInfos().clear();
        }else{
            msgTemp = msg;
        }
        try {
            msg = mapper.readValue(o.toString(), Msg.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (msg == null)
            return;
        if (msg.getData().getMessageInfos() == null)
            return;
        if (msg.isFlag()){
            if (msgTemp!=null){
                for (int i = 0; i < msgTemp.getData().getMessageInfos().size(); i++) {
                    msg.getData().getMessageInfos().add(msgTemp.getData().getMessageInfos().get(i));
                }
            }
        }else{
            pageNum = currentPage;
        }

        msgListAdapter = new MsgListAdapter(getActivity(),msg.getData().getMessageInfos());
        mRecyclerView.setAdapter(msgListAdapter);
        msgListAdapter.notifyDataSetChanged();

    }


//    public List<Msg> getData(int randDate){
//        List<Msg> list=new ArrayList<>();
//        int endDate=randDate+10;
//        for (int i = randDate; i < endDate; i++) {
//            Msg msg=new Msg();
//            msg.setMsg_content("2015年 美丽田园重磅来袭！部分商品 5折起售 让你清凉一夏 赶快行动吧！赶快行动 赶快行动吧");
//            msg.setMsg_id(i);
//            msg.setMsg_title("资讯"+i);
//            list.add(msg);
//        }
//        return list;
//    }


//    @UiThread(delay = 3000)
//    void initiateRefresh(){
//        msgListAdapter.notifyDataSetChanged();
//
//        mSwipeRefreshLayout.setRefreshing(false);
//    }



    @Click({R.id.back_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstace().getSupportFragmentManager().popBackStack();
                break;
        }
    }

    @Override
    public void onRefresh() {
        if (!mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(true);
        }
//        List<Msg> t=getData((int)(Math.random()*100));
//        temps.clear();
//        temps.addAll(t);
//
//        temps.addAll(list);
//        list.addAll(t);

//        msgListAdapter.notifyDataSetChanged();
//        initiateRefresh();
        pageNum = 1;
        getData();
    }

    @Override
    public void onLoad() {
        if (!swipeRefreshLayout.isLoading()) {
            swipeRefreshLayout.setLoading(true);
        }
        pageNum++;
        getData();
    }
}
